public class demo1 {
    boolean[][] cow=new boolean[9][10];
    boolean[][] low=new boolean[9][10];
    boolean[][][] cricle=new boolean[3][3][10];
    public void solveSudoku(char[][] board) {
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]!='.'){
                    int n=board[i][j]-'0';
                    cow[j][n]=true;
                    low[i][n]=true;
                    cricle[i/3][j/3][n]=true;
                }
            }
        }
        dfs(board);
    }
    //利用boolean的特性来判断进行添加还是回溯
    public boolean dfs(char[][]board){
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]=='.'){
                    //填数
                    for(int sum=1;sum<10;sum++){
                        if(!cow[j][sum]&&!low[i][sum]
                                &&!cricle[i/3][j/3][sum]){
                            board[i][j]=(char)('0'+sum);
                            cow[j][sum]=low[i][sum]= cricle[i/3][j/3][sum]=true;
                            //符合条件就继续 不符合就回溯
                            if(dfs(board))return true;
                            cow[j][sum]=low[i][sum]= cricle[i/3][j/3][sum]=false;
                            board[i][j]='.';
                        }
                    }
                    return false;//sum没有进入true 说明不符和
                }
            }
        }
        return true;
    }
}
